package com.jerei.utils;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;

/**
 * Created by sunfeihu on 2016/10/14.
 */
public class UtilsZip {

    public static String gzip(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
        return out.toString("ISO-8859-1");
    }


    public static String ungzip(String str) throws UnsupportedEncodingException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
        try {
            GZIPInputStream gunzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = gunzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            str = out.toString();
        } catch (Exception e) {

        }
        return str;
    }

    public static void compress(File file, File zipFile) {
        if (!file.exists())
            return;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
            CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
            ZipOutputStream out = new ZipOutputStream(cos);
            compress(file, out);
            out.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void compress(File file, ZipOutputStream out) {
        /* 判断是目录还是文件 */
        if (file.isDirectory()) {
            compressDirectory(file, out);
        } else {
            compressFile(file, out);
        }
    }

    public static void compressDirectory(File dir, ZipOutputStream out) {
        if (!dir.exists()) {
            return;
        }
        File[] files = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.endsWith(".jrdata")) {
                    return true;
                }
                return false;
            }

        });
        for (int i = 0; i < files.length; i++) {
            compress(files[i], out);
        }
    }

    public static void compressFile(File file, ZipOutputStream out) {
        if (!file.exists()) {
            return;
        }
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(file.getName());
            out.putNextEntry(entry);
            int count;
            byte data[] = new byte[8192];
            while ((count = bis.read(data, 0, 8192)) != -1) {
                out.write(data, 0, count);
            }
            bis.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void deCompression(String zipFile, String destination) throws IOException {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration en = zip.entries();
        ZipEntry entry = null;
        byte[] buffer = new byte[8192];
        int length = -1;
        InputStream input = null;
        BufferedOutputStream bos = null;
        File file = null;

        while (en.hasMoreElements()) {
            entry = (ZipEntry) en.nextElement();
            if (entry.isDirectory()) {
                continue;
            }
            input = zip.getInputStream(entry);
            file = new File(destination, entry.getName());
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            bos = new BufferedOutputStream(new FileOutputStream(file));
            while (true) {
                length = input.read(buffer);
                if (length == -1)
                    break;
                bos.write(buffer, 0, length);
            }
            bos.close();
            input.close();
        }
        zip.close();
    }

}
